<template>
  <div class="container">
    <div class="content-wrapper" ref="scrollcontainer" v-infinite-scroll="loadData" infinite-scroll-disabled="disabled" infinite-scroll-immediate="false">
      <el-card class="box-card" v-for="item in uncompleteList" :key="item.id">
        <div @click="showMenu(item)" style="padding: 20px;">
          <div class="rows">
            <span class="title">{{item.warehousingBelongName}}</span>
          </div>
          <div class="rows" v-if="item.warehousingBelongId !== '1293102120648929281'">
            <span><span class="item-name">单据编号：</span>{{item.warehousingPlanId}}</span>
            <span><span class="item-name">产品编号：</span>{{item.warehousingProductId}}</span>
          </div>
          <div class="rows">
            <span><span class="item-name">申请时间：</span>{{item.warehousingCreateTime}}</span>
            <span><span class="item-name">申请人：</span>{{item.warehousingCreatorName}}</span>
          </div>
          <div class="rows">
            <span><span class="item-name">状态：</span><span v-if="item.dataType === '1'">{{warehousingStatus[Number(item.warehousingStatus)-1]}}</span><span v-else>{{warehousingStatus2[Number(item.warehousingStatus)-1]}}</span></span>
            <span><span class="item-name">处理人：</span><span>{{item.warehousingStatus === '1' ? item.warehousingQualityAuditName : item.warehousingStatus === '2' ? item.warehousingSignForName : item.warehousingStatus === '3' ? item.warehousingInputName : ''}}</span></span>
          </div>
        </div>
      </el-card>
      <p v-if="loading" class="tips">加载中...</p>
      <p v-if="noMore" class="tips">没有更多了</p>
    </div>
    <Tab currentTab="uncompleteList"/>
  </div>
</template>

<script>
import dingMixin from  '@/mixins/ding'
import formUtil from '@/utils/formUtil'
import Tab from './component/Tab'
export default {
  name: '',
  data () {
    return {
      currentScrollTop: 0,
      uncompleteList: [],
      page: 1,
      totalPage: 1,
      pageSize: 10,
      isFirstLoading: true,
      loading: false,
      warehousingStatus: ['待品质审批', '待签收', '待erp录入', 'erp录入完成'],
      warehousingStatus2: ['待部门主管审批', '待中心主管审批', '待薪酬专员核实', '已发放'],
    };
  },
  components: {
    Tab
  },
  mixins: [dingMixin],
  computed: {
    disabled () {
      return this.isFirstLoading || this.loading || this.noMore
    },
    noMore () {
      return this.page >= this.totalPage;
    }
  },
  beforeRouteLeave(to,from,next) {
    this.currentScrollTop = this.$refs.scrollcontainer.scrollTop;
    this.mobileStopRefresh()
    next()
  },
  activated() {
    this.mobileGetDevice(({model, brand, version}) => {
      if(brand === 'iPhone') {
        this.mobildPullToRefresh(() => {
          this.getUncompleteList(true)
        });
      }
    })
    if(window.NEEDREFRESH1) {
      this.getUncompleteList(true);
      window.NEEDREFRESH1 = false;
    } else {
      this.$refs.scrollcontainer.scrollTop = this.currentScrollTop;
    }
  },
  mounted() {
    this.getUncompleteList(true);
    if(this.$route.params.id) {
      this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: this.$route.params.id, warehousingBelongId: this.$route.params.warehousingBelongId, warehousingBelongName: '', operation: 'check', planNumber: ''}})
    }
  },
  methods: {
    loadData() {
      this.loading = true;
      this.page ++;
      this.getUncompleteList();
    },
    async getUncompleteList(init) {
      if(init) {
        this.page = 1;
        this.uncompleteList = [];
        this.isFirstLoading = true;
      }
      let {list, totalPage} = await this.$httpN.postB('pc/warehousing/uncompleteList', {
        page: this.page,
        limit: this.pageSize,
      });
      this.loading = false;
      this.uncompleteList.push(...list)
      if(init) {
        this.totalPage = totalPage;
        this.$nextTick(() => {
          this.isFirstLoading = false;
        })
      }
    },
    showMenu(item) {
      this.mobileSelect('更多操作',['编辑','审核', '查看', '删除'], ({buttonIndex}) =>{
        if(buttonIndex === 0) {
          if(item.canModifyData !== '1') {
            return this.mobileAlert('你无法编辑')
          }
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: item.warehousingBelongId, warehousingBelongName: item.warehousingBelongName, operation: 'edit', planNumber: ''}})
        }else if(buttonIndex === 1) {
          if(!((item.warehousingStatus === '1' && this.userInfo.dingId === item.warehousingQualityAuditId) || 
            (item.warehousingStatus === '2' && this.userInfo.dingId === item.warehousingSignForId) || 
            (item.warehousingStatus === '3' && this.userInfo.dingId === item.warehousingInputId))) {
            return this.mobileAlert('你无法审核');
          }
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: item.warehousingBelongId, warehousingBelongName: item.warehousingBelongName, operation: 'check', planNumber: ''}})
        }else if(buttonIndex === 2) {
          this.$router.push({ name: 'mpiecework-storageAddOrEdit', params: {id: item.id, warehousingBelongId: item.warehousingBelongId, warehousingBelongName: item.warehousingBelongName, operation: 'view', planNumber: ''}})
        }else if(buttonIndex === 3) {
          if(item.canDeleteData !== '1') return this.mobileAlert('你无法删除')
          this.delData(item)
        }
      })
    },
    async delData(data) {
      formUtil.submitConfirm('请问您是否确定删除这条记录？', async ()=> {
        if(!await this.$httpN.postB(!this.isTime ? 'pc/warehousing/delete' : 'pc/subsidy/delete', {ids: data.id, warehousingBelongName: data.warehousingBelongName})) return;
        formUtil.submitSuccess('删除成功');
        this.getUncompleteList(true);
      })
    }
  }
}
</script>

<style lang='scss' scoped>
.container {
  height: calc(100vh - 46px);
  background-color: #F8F8F8;
  .content-wrapper {
    height: 100%;
    overflow-y: auto;
    padding: 10px 18px;
    .box-card {
      margin-bottom: 10px;
      line-height: 16px;
      &:last-child {
        margin-bottom: 0;
      }
      /deep/ .el-card__body {
        padding: 0;
      }
      .rows {
        margin-bottom: 12px;
        font-size: 14px;
        color: #303334;
        display: flex;
        justify-content: space-between;
        &:last-child {
          margin-bottom: 0;
        }
        .item-name {
          color: #AEAEB0;
        }
      }
      .title {
        font-weight: 700;
        font-size: 16px;
      }
    }
    .tips {
      text-align: center;
      color: #909399;
    }
  }
}
</style>